package formulation.controllers;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import db.fortifier.Fortifier;
import db.fortifier.FortifierService;
import db.nutriment.Nutriment;
import db.nutriment.NutrimentService;

public class FortifierController extends MultiActionController
{
    private FortifierService fortifierService;
    private NutrimentService nutrimentService;
    
    public void setFortifierService(FortifierService fortifierService) {
        this.fortifierService = fortifierService;
    }
    
    public void setNutrimentService(NutrimentService nutrimentService) {
        this.nutrimentService = nutrimentService;
    }
    // administrator methods
    public ModelAndView admin(HttpServletRequest request,
            HttpServletResponse response)
    {
        request.setAttribute("nutriments", nutrimentService.list());
        return new ModelAndView("fortifier/admin/fortifier", "fortifiers", fortifierService.list());
    }
    
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response, Fortifier fortifier)
    {
        fortifierService.insert(fortifier);
        return new ModelAndView("redirect:admin.do");
    }
    
    public ModelAndView remove(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long id = Long.parseLong(request.getParameter("id"));
        fortifierService.remove(id);
        return new ModelAndView("redirect:admin.do");
    }
    // end
    public ModelAndView listNutriment(HttpServletRequest request,
            HttpServletResponse response, Fortifier fortifier)
    {
        Set<Nutriment> nutriments = fortifierService.listNutriment(fortifier.getId());
        return new ModelAndView("fortifier/show-nutriment", "nutriments", nutriments);
    }
    
    public ModelAndView addNutriment(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long fortifierId = Long.valueOf(request.getParameter("fortifierId"));
        Long nutrimentId = Long.valueOf(request.getParameter("nutrimentId"));
        fortifierService.addNutriment(fortifierId, nutrimentId);//*******
        return new ModelAndView("redirect:admin.do", "fortifiers", fortifierService.list());
    }
    
    public ModelAndView addFortifierWithNutriment(HttpServletRequest request,
    		HttpServletResponse response, Fortifier fortifier)
    {
        Long nutrimentId = Long.valueOf(request.getParameter("nutrimentId"));
        Nutriment nutriment = nutrimentService.find(nutrimentId);
        HashSet<Nutriment> nutriments = new HashSet<Nutriment>();
        nutriments.add(nutriment);
        fortifier.setNutriments(nutriments);
        fortifierService.insert(fortifier);
    	return new ModelAndView("redirect:/frame/fortifier_form.do");
    }
    
    public ModelAndView removeFortifier(HttpServletRequest request,
    		HttpServletResponse reponse, Fortifier fortifier)
    {
    	Long id = Long.parseLong(request.getParameter("id"));
        fortifierService.remove(id);
    	return new ModelAndView("redirect:/frame/fortifier_form.do");
    }
}
